﻿
using System;
using System.Collections.Generic;
using System.Linq;
using RBAC.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using RBAC.DAL.Models;

namespace RBAC.DAL.Dal
{
    public class DomainDAL
    {
        private readonly ISession _session;
        private readonly int _projectId;
        public DomainDAL(ISession session, int projectId)
        {
            _session = session;
            _projectId = projectId;
        }

        public List<Domain> GetAll(DomainSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(Domain));

            if (searchCriteria != null)
            {
                #region Search
                //Pk
                if (!string.IsNullOrEmpty(searchCriteria.DomainName))
                {
                    if (searchCriteria.EqPk.GetValueOrDefault())
                    {
                        criteria.Add(Restrictions.Eq("DomainName", searchCriteria.DomainName));
                    }
                    else
                    {
                        criteria.Add(Restrictions.Like("DomainName", searchCriteria.DomainName, MatchMode.Anywhere));
                    }
                }
                //lookup to parent
                if (searchCriteria.IsActive != null)
                {
                    criteria.Add(Restrictions.Eq("IsActive", searchCriteria.IsActive));
                }
                if (searchCriteria.ProjectId != null)
                {
                    criteria.Add(Restrictions.Eq("ProjectId", searchCriteria.ProjectId));
                }
                #endregion

                #region Sort
                if (searchCriteria.IsSort)
                    criteria.AddOrder(searchCriteria.Order);
                #endregion
            }
            return criteria.List<Domain>().ToList();
        }

        public void Add(Domain domain)
        {
            if (domain != null)
            {
                domain.CreateDate = DateTime.Now;
                domain.ProjectId = _projectId;
                //if (domain.CreateBy != null) domain.CreateBy = _session.Get<User>(domain.CreateBy.Id);
            }
            _session.Save(domain);
        }

        public void Update(Domain domain)
        {

            // chlidren

            var obj = _session.Get<Domain>(domain.Id);
            if (obj == null)
                throw new Exception("Update Not Success");

            domain.CreateDate = obj.CreateDate;
            domain.CreateById = obj.CreateById;
            domain.ProjectId = obj.ProjectId;
            domain.Functions = new List<Function>(obj.Functions);

            _session.Merge(domain);

        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE RBAC.DAL.Entities.Domain where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");
        }
    }
}

